"use strict";
var outtxt = 'aaa';

function btnquery_click() {
    //todo:选择随机数，某个条件随机一条记录
    var starnum = stars();
    //测试post响应，已经通过
    $.ajax({
        type: 'POST',
        url: 'writesql.php',
        data: { para_id: $("#txtid").val(), para_star: starnum, para_chinese: $("#txtchinese").val() },
        //success: success,
        dataType: 'JSON'
    });
    //选择符合条件的单词
    geturl();

}
function geturl() {
    $.ajax({
        url: 'readsql.php?id=' + $("#txttest").val() + '&check_star=' + $("#check_star").prop("checked") + '&check_lastdate=' + $("#check_lastdate").prop("checked") + '&star=' + $("#select_star").val() + '&lastdate=' + $("#select_lastdate").val() + '&lastnum=' + $("#check_lastnum").prop("checked") + '&radom=' + $("#check_radom").prop("checked"),
        type: 'get',
        dataType: 'JSON',
        success: function (data) {
            console.log(data);
            $("#txtid").val(data["id"]);
            if ($("#txtenglish").val() == data["english"]) {
                geturl();
                return 0;
            } else {
                $("#txtenglish").val(data["english"]);
                $("#txtchinese").val(data["chinese"]);
                //$("#txtlastdate").val(data["lastdate"]);
                //$("#txtlastnum").val(data["lastnum"]);
                fanyi(data["english"]);
                document.getElementById('divaudio').innerHTML = '<iframe id="frameaudio" src="http://dict.youdao.com/dictvoice?audio=' + data["english"] + '"></iframe>';
                var showstar = 5;
                if (data["star"] != null) {
                    showstar = data["star"];
                }
                btnstar_click(showstar);

            }

        }
    });
}

function btnstar_click(n) {
    for (var i = 1; i < 6; i++) {
        $("#btnstar" + i).css("color", "black");
    }
    for (var i = 1; i <= n; i++) {
        $("#btnstar" + i).css("color", "red");
    }
    $("#txtstar").val(stars());
}
//根据星星按钮的颜色判读星星的数量
function stars() {
    var star_n = 0;
    for (var i = 5; i > 0; i--) {
        if (rgb2hex($("#btnstar" + i).css("color")) == "#ff0000") {
            //console.log(i);
            return i;
        }
    }
}
//rgb转换为hex，便于比较颜色值
function rgb2hex(rgb) {
    rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);

    function hex(x) {
        return ("0" + parseInt(x).toString(16)).slice(-2);
    }
    return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}
$(document).ready(function () {
    $("#btntest").click(function () {
        console.log($("#select_star").val());
    });
    $("#check_lastdate").click(function () {
        if ($(this).prop("checked")) {
            $("#select_lastdate").prop("disabled", false);
        } else {
            $("#select_lastdate").prop("disabled", "disabled");
        }
    });
    $("#check_star").click(function () {
        if ($(this).prop("checked")) {
            $("#select_star").prop("disabled", false);
        } else {
            $("#select_star").prop("disabled", "disabled");
        }
    });
});
//调用百度翻译，异步的问题没有解决，所以没法用函数返回翻译结果，只能在内部直接把翻译结果赋值给文本框
function fanyi(intxt) {
    var appid = '20180718000186694';
    var key = '6hlRV5ytiy0S_8QKQRMJ';
    var salt = 1435660288; //(new Date).getTime();
    var query = 'apple';
    // 多个query可以用\n连接  如 query='apple\norange\nbanana\npear'
    var from = 'en';
    var to = 'zh';
    var str1 = appid + query + salt + key;
    var sign = MD5(str1);
    //var intxt = document.getElementById("txt01").value;
    var inmd5 = appid + intxt + salt + key;
    var outmd5 = MD5(inmd5);
    var httpadrs = "https://api.fanyi.baidu.com/api/trans/vip/translate?q=" + intxt + "&from=en&to=zh&appid=20180718000186694&salt=1435660288&sign=" + outmd5;
    var outtxt = 'false';
    $.ajax({
        url: httpadrs,
        type: 'GET',
        dataType: 'JSONP',
        async: false,
        success: function (data) {
            //提取json中的汉语翻译部分
            outtxt = data.trans_result[0].dst;
            document.getElementById("wordzh").value = outtxt;
        }
    });
}
//显示详细解释
function showdetail() {
    if ($("#btndetail").val() == '详细解释') {
        $("#btndetail").val("隐藏解释");
        document.getElementById('divfanyi').innerHTML = '<iframe id="framefanyi" src="https://m.youdao.com/dict?le=eng&q=' + document.getElementById('txtenglish').value + '"></iframe>';
    }
    else{
        $("#btndetail").val("详细解释");
        hidedetail();
    }
}
function hidedetail() {
    document.getElementById('divfanyi').innerHTML = '';
}
